package com.ewzj.bigdata.mvp.model

import android.app.Application
import com.ewzj.bigdata.app.config.netmodel.CommonCache
import com.ewzj.bigdata.app.config.netmodel.CommonService
import com.ewzj.bigdata.app.utils.Apputils
import com.ewzj.bigdata.mvp.contract.OrginNewsFragmentContract
import com.ewzj.bigdata.mvp.model.reqbean.Reqpagebean
import com.ewzj.bigdata.mvp.model.rspbean.BaseData
import com.ewzj.bigdata.mvp.model.rspbean.BaseListData
import com.ewzj.bigdata.mvp.model.rspbean.RspOriginBean
import com.google.gson.Gson
import com.jess.arms.integration.IRepositoryManager
import com.jess.arms.mvp.BaseModel
import com.jess.arms.utils.DeviceUtils
import io.reactivex.Observable
import io.rx_cache2.DynamicKey
import io.rx_cache2.EvictDynamicKey
import javax.inject.Inject

/**
 * author:zhangjing
 * 作用:
 * return:
 */
class OrginNewsFragmentModel @Inject constructor(val context:Application,val mGson: Gson, repositoryManager: IRepositoryManager):BaseModel(repositoryManager),OrginNewsFragmentContract.Model{


    val service:CommonService by lazy { repositoryManager.obtainRetrofitService(CommonService::class.java) }
    val cacheservice:CommonCache by lazy { repositoryManager.obtainCacheService(CommonCache::class.java) }

    override fun getnewsByPageFromNet(pagebean: Reqpagebean): Observable<BaseData<BaseListData<RspOriginBean>>> {
        return service.orginTopicV3(Apputils.Utils.json2Requestbody(mGson.toJson(pagebean)))
    }

    override fun getnewsByPage(pagebean: Reqpagebean): Observable<List<RspOriginBean>> {
       return cacheservice.orginTopicV3(service.orginTopicV3(Apputils.Utils.json2Requestbody(mGson.toJson(pagebean))).map { t: BaseData<BaseListData<RspOriginBean>> ->t.data!!.list  }
       , DynamicKey(pagebean.p),EvictDynamicKey(DeviceUtils.hasInternet(context)))
    }

}